package com.koron.system.app.controller;

import com.koron.bean.app.AppBean;
import com.koron.bean.base.BaseQueryBean;
import com.koron.bean.base.Response;
import com.koron.bean.system.role.RoleBean;
import com.koron.bean.system.role.UserOwnJurisdictionBean;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.annocation.CasUser;
import com.koron.common.core.business.system.MyRSAUtil;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.system.app.service.AppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName AppController
 * @description
 * @Author zhouj
 * @Date 2021/7/13 10:37
 */
@Api(tags = "应用管理")
@RestController
@RequestMapping("/system/app")
public class AppController {

    @Autowired
    AppService appService;

    @ApiOperation(value = "新增应用")
    @PostMapping("/insertApp")
    public Response insertApp(@RequestBody @Validated AppBean appBean, @CasUser EamUser user) {
        String decrypt = MyRSAUtil.decrypt(appBean.getDbPassword());
        appBean.setDbPassword(decrypt);
        return Response.success(appService.inUpApp(appBean, user));
    }

    @ApiOperation(value = "修改应用")
    @PutMapping("/updateApp")
    public Response updateApp(@RequestBody @Validated AppBean appBean, @CasUser EamUser user) {
        String decrypt = MyRSAUtil.decrypt(appBean.getDbPassword());
        appBean.setDbPassword(decrypt);
        return Response.success(appService.updateApp(appBean, user));
    }

    @ApiOperation(value = "删除应用")
    @DeleteMapping("/delete")
    public Response deleteApp(String id) {
        return Response.success("删除成功", appService.deleteApp(id));
    }

    @ApiOperation(value = "删除应用list")
    @DeleteMapping("/deleteList")
    public Response deleteApp(@RequestParam("ids") List<String> ids) {
        return Response.success("删除成功", appService.deleteAppList(ids));
    }

    @ApiOperation(value = "查询应用列表")
    @GetMapping("/queryAppList")
    public Response queryAppList(AppBean appBean, BaseQueryBean queryBean) {
        List<AppBean> appBeanList = appService.queryAppList(appBean, queryBean);
        Long count = appService.queryAppListCount(appBean);
        return Response.success(appBeanList, count);
    }

    @ApiOperation(value = "查询单个应用信息")
    @GetMapping("/querySingleApp")
    public Response querySingleApp(AppBean appBean) {
        //判断是否有超级管理员的角色
        if (!isSuperAdmin()) {
            Response.fail("没有权限访问");
        }
        return Response.success(appService.querySingleApp(appBean));
    }

    public Boolean isSuperAdmin() {
        UserOwnJurisdictionBean userOwnJurisdictionBean = ThreadLocalContext.get().getUserOwnJurisdictionBean();
        if (!Objects.isNull(userOwnJurisdictionBean)) {
            List<RoleBean> roleBean = userOwnJurisdictionBean.getRoleBean();
            if (CollectionUtils.isNotEmpty(roleBean)) {
                for (RoleBean bean : roleBean) {
                    if ("role_root".equals(bean.getRoleType())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @ApiOperation(value = "启用/停用应用")
    @GetMapping("/startOrStopApp")
    public Response startOrStopApp(@RequestParam("ids") List<String> ids, @RequestParam("status") Integer status) {
        return Response.success("操作成功", appService.startOrStopApp(ids, status));
    }

}
